#pragma once

// Copyright (c) 2010 Manuel Peinado Gallego <manuel.peinado@gmail.com>
// Distributed under the MIT license

// SceneTreeCtrl.h : interface of the CSceneTreeCtrl class
//

#include "ViewTree.h"
#include "UiFollower.h"
#include <map>
class Selectable;


class CSceneTreeCtrl : public CViewTree, 
                       public WorldFollower, 
                       public UiFollower
{
public:
    
    CSceneTreeCtrl();
    virtual ~CSceneTreeCtrl();

protected:

    // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

    HTREEITEM m_shadersTreeItem;
    HTREEITEM m_texturesTreeItem;
    HTREEITEM m_meshesTreeItem;
    HTREEITEM m_meshBuffersTreeItem;

    // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);

    // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    // Implementation of Selection/Material/NodeListener. 

    void onSelectionChanged(SelectionManager * sender) override;
    void onNodeEvent(const NodeEvent&) override;
    void onMaterialEvent(const MaterialEvent& event) override;
    void onShaderEvent(const ShaderEvent&) override;
    void onTextureAddedOrRemoved(TextureManager* sender, 
                                 Texture* tex) override;
    void onMeshEvent(const MeshEvent&) override;

    // WorldFollower override
    void onWorldChanged(World* newWorld) override;

    // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    
    // See http://bit.ly/99aaCB (note enhacements in comments)
    CImageList*	m_pDragImage;
    BOOL		m_bLDragging;
    HTREEITEM	m_hitemDrag, m_hitemDrop, m_hitemForceExpand;
    
    std::map<Selectable*, HTREEITEM> m_selectionToItem;

    void setItemSelectable(Selectable* selectable, HTREEITEM item);
    void update();
    void updateSelection();
    HTREEITEM addNode(Node& node, const HTREEITEM item);
    void build(Node & root, const HTREEITEM item);

    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnTvnBegindrag(NMHDR *pNMHDR, LRESULT *pResult);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult);
    afx_msg void OnRClick(NMHDR* pNMHDR, LRESULT* pResult);
};

